
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
//import 'package:fluttertoast/fluttertoast.dart';
import 'package:jubobao/home/JuBoNewWebView.dart';
import 'package:jubobao/other/Tool/JuBoToolManager.dart';
import 'package:jubobao/other/Widgets/JuBoCommonWidget.dart';
import 'package:jubobao/provide/JuBoLoginStateManager.dart';
import 'package:provide/provide.dart';
import 'package:shared_preferences/shared_preferences.dart';

typedef JuBoLoginSuccess = Future Function();

class JuBoLogin extends StatelessWidget {

  TextEditingController  _phoneTextField = TextEditingController();
  TextEditingController  _passwordTextField = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        FocusScope.of(context).requestFocus(FocusNode());
      },

      child: Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Color.fromRGBO(244, 244, 244, 1),
        appBar: AppBar(title: Text('登录'),),
        body: Column(
           children: <Widget>[
             Expanded(flex: 10,child: phoneAndPassword()),
             Expanded(flex: 15,child: operationArea(context)),
             Expanded(flex: 20,child: thirdPartyLogin()),
             Expanded(flex: 5,child: loginInstructions(context))
           ],
        )
      ),
    );
  }

  // 账号密码输入框
  Widget phoneAndPassword (){
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
          Container(
            padding: EdgeInsets.symmetric(horizontal: 8),
            color: Colors.white,
            child: TextField(
              controller: _phoneTextField,
              decoration: InputDecoration(
                icon: Icon(Icons.phone),
                hintText: '请输入账号',
                border: InputBorder.none,
              ),
              autofocus: false,
            ),
          ),
          Divider(height: 0),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 8),
            color: Colors.white,
            child: TextField(
              controller: _passwordTextField,
              obscureText: true,
               decoration: InputDecoration(
                  icon: Icon(Icons.lock_outline),
                  border: InputBorder.none,
                  hintText: '请输入密码',
              ),
              autofocus: false,
            ),
          ),
      ],
    );
  }

  // 操作区
  Widget operationArea (ctx) {
      return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
              Container(
                width: double.maxFinite,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                    color: Colors.blue,
                ),
                child: MaterialButton(
                  child: Text('登录',style: TextStyle(color: Colors.white),),
                  onPressed: (){
                    if(_phoneTextField.value.text.length == 0){
//                        Fluttertoast.showToast(msg: '请输入正确的手机号',gravity: ToastGravity.CENTER);
                        return;
                    }

                    if(_passwordTextField.value.text.length == 0){
//                      Fluttertoast.showToast(msg: '请输入密码',gravity: ToastGravity.CENTER);
                      return;
                    }
                    Provide.value<JuBoLoginState>(ctx).changeState(true);
                    JuBoToolManager.setLoginState(true);
                  },
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  MaterialButton(child: Text('忘记密码',style: TextStyle(color: Colors.blue),),),
                  MaterialButton(child: Text('新用户注册',style: TextStyle(color: Colors.blue),),),
                ],
              )
           ],
        ),
      );
  }

  // 第三方登录区域
  Widget thirdPartyLogin (){

    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Divider(height: 10.0,indent: 60.0,color: Colors.red,),
              Text('第三方登录账号'),
              Divider(height: 1.0,endIndent: 60.0,color: Colors.red,),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
                JuBoWidget.imageTitle('assets/login/qq_dl_icon.png', 'QQ',40),
                JuBoWidget.imageTitle('assets/login/weibo_dl_icon.png', '微信',40),
                JuBoWidget.imageTitle('assets/login/weixin_dl_icon.png', '微博',40),
            ],
          )
      ],
    );
  }

  // 登录说明
  Widget loginInstructions (context){
      return RichText(
          text: TextSpan(
//           text: '登录即代表阅读并同意服务条款',
             style: TextStyle(
               color: Colors.black87
             ),
             children: [
               TextSpan(
                   text: '登录即代表阅读并同意',
               ),
               TextSpan(
                   text: '服务条款',
                   style: TextStyle(color: Colors.blue),
                   recognizer: TapGestureRecognizer()
                                               ..onTap = (){
                                                  print('服务条款');

                                                  Navigator.of(context).push(MaterialPageRoute(
                                                    builder: (ctx){
                                                      return JuBoNewWebView('http://www.atjubo.com/serviceagree/webapp/views/JBW.aspx');
                                                    }
                                                  ));

                                               }
                )
             ]
      ));
  }

}

